package com.twu.biblioteca;

import java.util.Scanner;

/**
 * Created by elimydlarz on 15/04/14.
 * TODO Keep list of checked out books and movies with User and reciprocal link in Borrowable
 */
public class Librarian {

    private final String lineBreak = System.getProperty("line.separator");

    private final UserManager userManager;
    private final Library library;

    public Librarian() {
        userManager = new UserManager();
        library = new Library();
    }

    public Librarian(UserManager userManager, Library library) {
        this.userManager = userManager;
        this.library = library;
    }

    public void writeWelcomeMessage() {
        System.out.print("Welcome to Biblioteca.");
        System.out.print(lineBreak);
    }

    private void writeMenuOptions() {
        System.out.print("Main menu: ");
        System.out.print(lineBreak);
        System.out.print("List books");
        System.out.print(lineBreak);
        System.out.print("List movies");
        System.out.print(lineBreak);
        if (userManager.userIsLoggedIn()) {
            System.out.print("Check out book");
            System.out.print(lineBreak);
            System.out.print("Check out movie");
            System.out.print(lineBreak);
            System.out.print("Return book");
            System.out.print(lineBreak);
            System.out.print("Return movie");
            System.out.print(lineBreak);
            System.out.print("User info");
            System.out.print(lineBreak);
            System.out.print("Log out");
            System.out.print(lineBreak);
        }
        else {
            System.out.print("Log in");
            System.out.print(lineBreak);
        }
        System.out.print("Quit");
        System.out.print(lineBreak);
    }

    public void mainMenu() {
        Boolean programContinues = true;
        while (programContinues) {
            writeMenuOptions();
            Scanner userInput = new Scanner(System.in);
            String menuInput = userInput.nextLine();
            if (menuInput.equalsIgnoreCase("list books")) {
                writeBookTitles();
            }
            else if (menuInput.equalsIgnoreCase("list movies")) {
                writeMovieTitles();
            }
            else if (menuInput.equalsIgnoreCase("check out book") && userManager.userIsLoggedIn()) {
                helpCheckOutBook();
            }
            else if (menuInput.equalsIgnoreCase("check out movie") && userManager.userIsLoggedIn()) {
                helpCheckOutMovie();
            }
            else if (menuInput.equalsIgnoreCase("return book") && userManager.userIsLoggedIn()) {
                helpReturnBook();
            }
            else if (menuInput.equalsIgnoreCase("return movie") && userManager.userIsLoggedIn()) {
                helpReturnMovie();
            }
            else if (menuInput.equalsIgnoreCase("user info") && userManager.userIsLoggedIn()) {
                writeUserInfo();
            }
            else if (menuInput.equalsIgnoreCase("log out") && userManager.userIsLoggedIn()) {
                helpLogOut();
            }
            else if (menuInput.equalsIgnoreCase("log in") && userManager.userIsLoggedIn() == false) {
                helpLogIn();
            }
            else if (menuInput.equalsIgnoreCase("quit")) {
                programContinues = false;
                if (userManager.userIsLoggedIn()) userManager.attemptLogOut();
            }
            else {
                System.out.print("Select a valid option!");
                System.out.print(lineBreak);
            }
        }

        System.out.print("Bye.");
        System.out.print(lineBreak);
    }

    private void writeBookTitles() {
        for (String bookTitle : library.getBookTitles()) {
            System.out.print(bookTitle);
            System.out.print(lineBreak);
        }
    }

    private void writeMovieTitles() {
        for (String movieTitle : library.getMovieTitles()) {
            System.out.print(movieTitle);
            System.out.print(lineBreak);
        }
    }

    private void helpCheckOutBook() {
        Scanner userInput = new Scanner(System.in);
        String keyOfBookToCheckOut = userInput.nextLine();
        if (library.checkOutBook(keyOfBookToCheckOut)) {
            System.out.print("Thank you! Enjoy the book.");
            System.out.print(lineBreak);
        }
        else {
            System.out.print("That book is not available.");
            System.out.print(lineBreak);
        }
    }

    private void helpCheckOutMovie() {
        Scanner userInput = new Scanner(System.in);
        String keyOfMovieToCheckOut = userInput.nextLine();
        if (library.checkOutMovie(keyOfMovieToCheckOut)) {
            System.out.print("Thank you! Enjoy the movie.");
            System.out.print(lineBreak);
        }
        else {
            System.out.print("That movie is not available.");
            System.out.print(lineBreak);
        }
    }

    private void helpReturnBook() {
        Scanner userInput = new Scanner(System.in);
        String keyOfBookToReturn = userInput.nextLine();
        if (library.returnBook(keyOfBookToReturn)) {
            System.out.print("Thank you for returning the book.");
            System.out.print(lineBreak);
        }
        else {
            System.out.print("That book is not available.");
            System.out.print(lineBreak);
        }
    }

    private void helpReturnMovie() {
        Scanner userInput = new Scanner(System.in);
        String keyOfMovieToReturn = userInput.nextLine();
        if (library.returnMovie(keyOfMovieToReturn)) {
            System.out.print("Thank you for returning the movie.");
            System.out.print(lineBreak);
        }
        else {
            System.out.print("That movie is not available.");
            System.out.print(lineBreak);
        }
    }

    private void writeUserInfo() {
        User userToWrite = userManager.getActiveUser();
        System.out.print(userToWrite.getName());
        System.out.print(lineBreak);
        System.out.print(userToWrite.getPhone());
        System.out.print(lineBreak);
        System.out.print(userToWrite.getEmail());
        System.out.print(lineBreak);
    }

    private void helpLogOut() {
        if (userManager.attemptLogOut()) {
            System.out.print("Logged out.");
            System.out.print(lineBreak);
        }
        else {
            System.out.print("Can't log out as nobody was logged in.");
            System.out.print(lineBreak);
        }
    }

    private void helpLogIn() {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Enter your ID: ");
        System.out.print(lineBreak);
        String userKeyEntered = userInput.nextLine();
        System.out.print("Enter your password: ");
        System.out.print(lineBreak);
        String passwordEntered = userInput.nextLine();
        if (userManager.attemptLogIn(userKeyEntered, passwordEntered)) {
            System.out.print("Logged in.");
            System.out.print(lineBreak);
        }
        else {
            System.out.print("Log in failed.");
            System.out.print(lineBreak);
        }
    }

}
